package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AlpContractDetail;
import com.ruoyi.system.domain.PivotRequest;
import com.ruoyi.system.domain.vo.AlpContractDetailWithWeek;
import com.ruoyi.system.domain.vo.AlpContractDetailWithWeekHZ;
import com.ruoyi.system.service.IAlpAuthorityService;
import com.ruoyi.system.service.IAlpContractDetailService;
import com.ruoyi.system.service.IAlpWeeklyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * @fileName: AlpWeeklyController
 * @description: TODO
 * @author: Li Wenbo
 * @date: 2020-3-2 14:46
 * @version: V1.0
 */
@Controller
@RequestMapping("/system/weekly")
public class AlpWeeklyController extends BaseController {

    private String prefix = "system/weekly";

    @Autowired
    private IAlpWeeklyService alpWeeklyService;
    @Autowired
    private IAlpContractDetailService alpContractDetailService;
    @Autowired
    private IAlpAuthorityService authorityFilter;

    @RequiresPermissions("system:weekly:view")
    @GetMapping()
    public String weeklyList(ModelMap mmap)
    {
        int week = DateUtils.getWeekOfYearByF(new Date(), 5);
        mmap.put("week", week);
        return prefix + "/weeklyList";
    }

    /**
     * 周汇总公司级
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/totalList")
    @ResponseBody
    public TableDataInfo totalList(){
        List<Map> list = alpWeeklyService.getTotalWeeklyList();
        return getDataTable(list);
    }

    /**
     * 周汇总部门级
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/deptList")
    @ResponseBody
    public TableDataInfo deptList(String deptName){
        String deptNames = authorityFilter.authorityDeptFilter(deptName);
        if (StringUtils.isEmpty(deptNames)) {
            List<Map> list = new ArrayList<>();
            return getDataTable(list);
        }
        List<Map> list = alpWeeklyService.getDeptWeeklyList(deptNames);
        return getDataTable(list);
    }


    /**
     * 周汇总个人级
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/personalList")
    @ResponseBody
    public TableDataInfo personalList(String personName){
        String personNames = authorityFilter.authorityPersonFilter(personName);
        if (StringUtils.isEmpty(personNames)) {
            List<Map> list = new ArrayList<>();
            return getDataTable(list);
        }
        List<Map> list = alpWeeklyService.getPersonalWeeklyList(personNames);
        return getDataTable(list);
    }

    /**
     * 周汇总项目级
     */
    @RequiresPermissions("system:weekly:contractList")
    @PostMapping("/contractList")
    @ResponseBody
    public TableDataInfo contractList(String contractName){
        List<Map> list = alpWeeklyService.getContractWeeklyList(contractName);
        return getDataTable(list);
    }

    /**
     * 周汇总数据穿透表跳转页
     */
    @GetMapping("/weeklyPivotTable")
    public String weeklyPivotTable(PivotRequest pivotRequest, ModelMap mmap)
    {
        mmap.put("pivotRequest", pivotRequest);
        return prefix + "/weeklyPivotTable";
    }

    /**
     * 周汇总数据穿透表
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/getPivotTable")
    @ResponseBody
    public TableDataInfo getPivotTable(PivotRequest pivotRequest){
        startPage();
        List<Map> list = alpWeeklyService.getWeeklyPivotTable(pivotRequest);
        return getDataTable(list);
    }

    /**
     * 周汇总项目级跳转页
     */
    @GetMapping("/toContractWeekly")
    public String toContractWeekly(ModelMap mmap)
    {
        int week = DateUtils.getWeekOfYearByF(new Date(), 5);
        mmap.put("week", week);
        return prefix + "/contractWeekly";
    }

    /**
     * 周汇总项目级
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/contractWeekly")
    @ResponseBody
    public TableDataInfo contractWeekly(AlpContractDetail alpContractDetail, String quarter, Integer weekNo){
        //startLayPage();
        startPage();
        List<Map> list = alpWeeklyService.getContractWeekly(alpContractDetail, quarter, weekNo);
        Integer count = alpWeeklyService.getWeeklyCount(alpContractDetail, weekNo);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(0);
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(count);
        return tableDataInfo;
    }

    /**
     * 导出周汇总表
     */
//    @RequiresPermissions("system:contractDetail:export")
//    @Log(title = "导出周汇总表", businessType = BusinessType.EXPORT)
//    @PostMapping("/exportcontractWeekly")
//    @ResponseBody
//    public AjaxResult exportcontractWeekly() {
//        AlpContractDetail alpContractDetail = new AlpContractDetail();
//        List<Map> list = alpWeeklyService.getContractWeekly(alpContractDetail, "", 0);
//    }

    /**
     * 获取季度对应的周
     */
    @GetMapping("/getWeekNoByQuarter")
    @ResponseBody
    public Object getWeekNoByQuarter(@RequestParam String quarter)
    {
        Calendar c = Calendar.getInstance();
        String year = String.valueOf(c.get(Calendar.YEAR));
        String startTime,endTime;
        Integer startWeek,endWeek;
        switch (quarter) {
            case "1" :
                startTime = year + "-01-01";
                endTime = year + "-03-31";
                break;
            case "2" :
                startTime = year + "-04-01";
                endTime = year + "-06-30";
                break;
            case "3" :
                startTime = year + "-07-01";
                endTime = year + "-09-30";
                break;
            case "4" :
                startTime = year + "-10-01";
                endTime = year + "-12-31";
                break;
            default:
                startTime = year + "-01-01";
                endTime = year + "-12-31";
        }
        startWeek = DateUtils.getWeekOfYearByF(DateUtils.parseDate(startTime), 5);
        endWeek = DateUtils.getWeekOfYearByF(DateUtils.parseDate(endTime), 5);
        JSONObject obj = new JSONObject();
        obj.put("startWeek", startWeek);
        obj.put("endWeek", endWeek);
        return obj;
    }
    @Log(title = "导出周汇总表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportcontractWeekly")
    @ResponseBody
    public AjaxResult exportcontractWeekly() {
        AlpContractDetail alpContractDetail = new AlpContractDetail();
        List<Map> list = alpWeeklyService.getContractWeekly(alpContractDetail, "", 0);

        List<AlpContractDetailWithWeekHZ> listBasic = new ArrayList<>();
        Field[] fields = AlpContractDetailWithWeekHZ.class.getDeclaredFields();

        for (Map map : list) {
            AlpContractDetailWithWeekHZ alpContractDetailWithWeek = new AlpContractDetailWithWeekHZ();
            alpContractDetailWithWeek.setContractNo((String) map.get("contractNo"));
            alpContractDetailWithWeek.setContractName((String) map.get("contractName"));
            alpContractDetailWithWeek.setXsPrincipalName((String) map.get("xsPrincipalName"));
            alpContractDetailWithWeek.setPartyAName((String) map.get("partyAName"));
            alpContractDetailWithWeek.setBalanceSince2020((BigDecimal) map.get("balanceSince2020"));
            alpContractDetailWithWeek.setWeek1((BigDecimal) map.get("W1"));

            try {
                for (int i = 1; i <= 53; i++) {
                    for (Field field : fields) {
                        String fieldName = field.getName();
                        if (!fieldName.startsWith("week")) {
                            continue;
                        }
                        if (i == Integer.valueOf(fieldName.substring(4))) {
                            field.set(alpContractDetailWithWeek,  map.get("W"+i));
                        }
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            listBasic.add(alpContractDetailWithWeek);
        }
        ExcelUtil<AlpContractDetailWithWeekHZ> util = new ExcelUtil<AlpContractDetailWithWeekHZ>(AlpContractDetailWithWeekHZ.class);
        return util.exportExcel(listBasic, "周汇总");
    }
}
